//
//  ViewController.m
//  17-文件管理沙盒目录
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"
#import "Book.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor redColor];
    // 沙盒(sandBox)
    // Document<文件文档> 储存持久性数据,用户<主动>数据存储,主要放用户信息`
    // Library <资源数据> 程序员管理的目录,用户<被动>存储数据,cache 缓存目录,存放应用缓存加载的资源,体积很大,需要经常清除
    // Preferences 设置用户的偏好设置,用NSUerdefault 存放,ios 自动备份该目录
    // temp <临时目录> 下载文件一般放这儿
    /**
     *  举个例子
     *
     *  @return 用户想下载一首歌曲
     歌曲先被播放,走cache,主动下载,先走temp,再走Document ,然后我设置该歌曲为喜爱,走Preferences 将歌曲名字加入该目录
     */
    
    // 获取沙河路径DocumentDirectory
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"%@",docPath);
    // 获取沙河路径LibraryDirectory的Caches 目录
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Caches"];
    NSLog(@"%@",cachePath);
    NSString *cache1Path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"%@",cache1Path);
    // 获取沙河路径
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"%@",tmpPath);
    /**
     *  NSUserDefaults的方法中用来记录一下永久保留的数据非常方便，不需要读写文件，而是保留到一个NSDictionary字典里，由系统保存到文件里，系统会保存到该应用下的/Library/Preferences/com.lanou3g.-7---------.plist文件中
     */
    [[NSUserDefaults standardUserDefaults] setObject:@"nima" forKey:@"zhale"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // 需要注意的是如果程序意外退出，NSUserDefaults standardUserDefaults数据不会被系统写入到该文件，不过可以使用［[NSUserDefaults standardUserDefaults] synchronize］调用synchronize函数将立即更新这些默认值，而不是等待程序终止，避免数据的丢失
    // 写入文件
    NSDictionary *dic = @{@"name":@"小傻逼", @"age":@"18", @"sex":@"男"};
    NSString *fileString = [docPath stringByAppendingPathComponent:@"dic.plist"];
    [dic writeToFile:fileString atomically:YES];
    // 读出文件
    NSDictionary *get_dic = [NSDictionary dictionaryWithContentsOfFile:fileString];
    NSLog(@"读取的字典为%@",get_dic);
    // Data类型
    UIImage *image = [UIImage imageNamed:@"1c2e84ad60dfe3a2e13024475009491e3462e1a3a003c-Uxaqu2_fw658"];
    NSData *data = UIImageJPEGRepresentation(image, 0.1); // 压缩
    NSString *dataPath = [docPath stringByAppendingPathComponent:@"imageData"];
    [data writeToFile:dataPath atomically:YES];
//    NSLog(@"data文件%@",[NSData dataWithContentsOfFile:dataPath]);
    
    
    // 复杂对象
    Book *bk = [Book new];
    bk.bookAddress = @"1";
    bk.bookAuthor = @"2";
    bk.bookName = @"宠物";
    bk.bookPrice = @"4";
    bk.bookType = @"5";
    NSString *bookPath = [docPath stringByAppendingPathComponent:@"book.plist"];
    BOOL isSuccess = [NSKeyedArchiver archiveRootObject:bk toFile:bookPath];
    NSLog(@"%@",bookPath);
    if (isSuccess) {
        NSLog(@"%@",[[NSKeyedUnarchiver unarchiveObjectWithFile:bookPath] bookName]);
    }
    NSArray *arr = @[@{
                         @"age" :@"10",
                         @"name" : @"nii"
                         },
                     @{@"age" :@"11",
                       @"name" : @"woo"}];
    NSString *arrayPath = [docPath stringByAppendingPathComponent:@"array.plist"];
    if ([NSKeyedArchiver archiveRootObject:arr toFile:arrayPath]) {
        NSArray *archieveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:arrayPath];
        for (NSDictionary *dic in archieveArray) {
            NSLog(@"%@",dic);
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
